import { decryptRes } from '../utils/requestUtil';
import {
  getNewUserData,
  getTodayNewPayUser,
  getTodaySNUser,
  getTodayActiUser,
  getTodayOrders,
  getTodayRev,
  sevenDaysMember,
  orderAndRevenue,
  sevenDaysApp,
  DAUAppRate,
} from '../services/analysisService';
import { message } from 'antd';
import moment from 'moment';

export default {
  namespace: 'analysis',

  state: {
    todayNewUser: { today: 0, ratios: 0 },
    todayNewPayUser: { today: 0, ratios: 0 },
    todaySNUser: { today: 0, ratios: 0 },
    todayActiUser: { today: 0, ratios: 0 },
    todayOrders: { today: 0, ratios: 0 },
    todayRev: { today: 0, ratios: 0 },

    sevenDaysMember: [],
    sevenDaysApp: [],
    DAUAppRate: 0,


    convData: [{ action: "注册会员", pv: 0, },
    { action: "关联小程序", pv: 0, },
    { action: "付费会员", pv: 0, }],

    orderAndRevenue: [],
    todayPercent: 0
  },

  effects: {
    *getNewUserData({ payload }, { call, put }) {
      console.log('请求参数???', payload);
      // const response = yield call(queryChart);
      const response = JSON.parse(decryptRes(yield call(getNewUserData, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'newUserDataReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    *getTodayNewPayUser({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(getTodayNewPayUser, payload)));

      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'todayNewPayUserReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    *getTodaySNUser({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(getTodaySNUser, payload)));

      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'todaySNUserReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    *getTodayActiUser({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(getTodayActiUser, payload)));

      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'todayActiUserReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    *getTodayOrders({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(getTodayOrders, payload)));

      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'todayOrdersReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    *getTodayRev({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(getTodayRev, payload)));

      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'todayRevReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *sevenDaysMember({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(sevenDaysMember, payload)));
      console.log('sevenDaysMember接口返回数据', response)
      if (response.code === 0) {
        let responseData = response.data;
        let data = [];
        let convData = [];
        let newUserTotal = 0;
        let unionUserTotal = 0;
        let payUserTotal = 0;
        if (responseData && responseData.length > 0) {
          for (let i = 0; i < responseData.length; i++) {
            let one = { date: responseData[i].date, newUser: responseData[i].sumData.newUser, unionUser: responseData[i].sumData.unionUser, payUser: responseData[i].sumData.payUser, }
            data.push(one);
            newUserTotal = newUserTotal + responseData[i].sumData.newUser;
            unionUserTotal = unionUserTotal + responseData[i].sumData.unionUser;
            payUserTotal = payUserTotal + responseData[i].sumData.payUser;
          }
        }

        convData.push({ action: '注册会员', pv: newUserTotal, percent: 100 });
        convData.push({ action: '关联小程序', pv: unionUserTotal, percent: newUserTotal ? (unionUserTotal / newUserTotal) : 0 });
        convData.push({ action: '付费会员', pv: payUserTotal, percent: newUserTotal ? (payUserTotal / newUserTotal) : 0 });


        yield put({
          type: 'sevenDaysMemberReducer',
          payload: { data, convData },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *orderAndRevenue({ payload }, { call, put }) {
      console.log('orderAndRevenue接口请求参数', payload)
      const response = JSON.parse(decryptRes(yield call(orderAndRevenue, payload)));
      console.log('orderAndRevenue接口返回数据', response)
      if (response.code === 0) {
        let responseData = response.data;
        let data = [];
        let total = 0;
        let today = 0;
        let todayPercent = 0;

        if (responseData && responseData.length > 0) {
          today = responseData[length - 1].sumData.ordersSum;
          for (let i = 0; i < responseData.length; i++) {
            let one = { date: responseData[i].date, quantity: responseData[i].sumData.ordersSum, revenue: responseData[i].sumData.revSum }
            data.push(one);
            total = total + responseData[i].sumData.revSum;
          }
          if (total) {
            todayPercent = today / total.toFixed(1);
          }
        } else {
          for (let i = 0; i < 7; i++) {
            data.push({ date: moment().subtract('days', i).format('YYYY-MM-DD'), quantity: 0, revenue: 0 });
          }
        }

        yield put({
          type: 'orderAndRevenueReducer',
          payload: { data, todayPercent },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },


    *sevenDaysApp({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(sevenDaysApp, payload)));
      console.log('sevenDaysAppr接口返回数据', response)
      if (response.code === 0) {
        let responseData = response.data;
        let data = [];

        if (responseData && responseData.length > 0) {
          for (let i = 0; i < responseData.length; i++) {
            let one = { date: responseData[i].date, education: responseData[i].sumData.education, book: responseData[i].sumData.book, video: responseData[i].sumData.video, }
            data.push(one);
          }
        }

        yield put({
          type: 'sevenDaysAppReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *DAUAppRate({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(DAUAppRate, payload)));
      console.log('sevenDaysMember接口返回数据', response)
      if (response.code === 0) {
        let responseData = response.data;
        let data = 0;

        if (responseData.revTodaySum && responseData.revLastSum) {
          data = (((responseData.revTodaySum / responseData.revLastSum).toFixed(2)) * 100)
        }

        yield put({
          type: 'DAUAppRateReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
  },

  reducers: {
    newUserDataReducer(state, action) {
      return {
        ...state,
        todayNewUser: action.payload.data,
      };
    },
    todayNewPayUserReducer(state, action) {
      return {
        ...state,
        todayNewPayUser: action.payload.data,
      };
    },
    todaySNUserReducer(state, action) {
      return {
        ...state,
        todaySNUser: action.payload.data,
      };
    },
    todayActiUserReducer(state, action) {
      return {
        ...state,
        todayActiUser: action.payload.data,
      };
    },
    todayOrdersReducer(state, action) {
      return {
        ...state,
        todayOrders: action.payload.data,
      };
    },
    todayRevReducer(state, action) {
      return {
        ...state,
        todayRev: action.payload.data,
      };
    },
    sevenDaysMemberReducer(state, action) {
      console.log('actionactionactionaction', action.payload.convData)
      return {
        ...state,
        sevenDaysMember: action.payload.data,
        convData: action.payload.convData,
      };
    },
    orderAndRevenueReducer(state, action) {
      return {
        ...state,
        orderAndRevenue: action.payload.data,
        todayPercent: action.payload.todayPercent,
      };
    },
    sevenDaysAppReducer(state, action) {
      return {
        ...state,
        sevenDaysApp: action.payload.data,
      };
    },
    DAUAppRateReducer(state, action) {
      return {
        ...state,
        DAUAppRate: action.payload.data,
      };
    },
    clear() {
      return {
        sevenDaysMember: [],
      };
    },
  },
};
